self.onmessage = function(e) {
	console.log("Received message:", e.data); // 检查整个消息对象
	if (!e || !e.data) {
		console.error("Invalid message received:", e);
		return self.postMessage({
			error: "Invalid message received"
		});
	}

	const {
		method,
		url,
		body
	} = e.data;

	if (!url) {
		return self.postMessage({
			error: "URL is required"
		});
	}

	let fetchOptions = {
		method: method,
		mode: "cors" // 确保跨域请求
	};

	if (method !== "GET") {
		fetchOptions.headers = {
			"Content-Type": "application/json"
		};
		fetchOptions.body = JSON.stringify(body); // 确保请求体是 JSON 格式
	}

	fetch(url, fetchOptions) // 使用 fetchOptions
		.then(response => {
				if (!response.ok) {
					throw new Error("Network response was not ok " + response.statusText); // 添加错误信息
				}else{
				// console.log(response);
				return response.json(); // 解析 JSON 响应
			}

		})
		.then(data => {
			console.log(data);
			self.postMessage(data);
		}) // 发送数据到主线程
		.catch(error => self.postMessage({
			error: error.message
		})); // 发送错误信息到主线程
};